//
//  ShowRoomListCollectionViewCell.m
//  XiRan-iOS
//
//  Created by 韩强 on 12/31/19.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "ShowRoomListCollectionViewCell.h"
@interface ShowRoomListCollectionViewCell ()

/// 图片
@property(nonatomic, strong) UIImageView *pictureIV;

/// 名称
@property(nonatomic, strong) UILabel *titleLB;

/// 查看更多的按钮
@property (nonatomic, strong) UIButton *lookMoreButton;
@end
@implementation ShowRoomListCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self lookMoreButton];
    }
    return self;
}

- (UIImageView *)pictureIV {
    if (_pictureIV == nil) {
        _pictureIV = [[UIImageView alloc] init];
        _pictureIV.contentMode = UIViewContentModeScaleAspectFit;
        _pictureIV.layer.cornerRadius = 5;
        _pictureIV.layer.masksToBounds = YES;
        [self.contentView addSubview:_pictureIV];
        
        [_pictureIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.left.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
            make.height.equalTo(@120);
        }];
    }
    return _pictureIV;
}

- (UILabel *)titleLB {
    if (_titleLB == nil) {
        _titleLB = [[UILabel alloc] init];
        _titleLB.font = MWFont(15);
        _titleLB.textColor = MWHexColor(@"#333333");
        [self.contentView addSubview:_titleLB];

        [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.pictureIV.mas_bottom).offset(5);
            make.left.equalTo(self.pictureIV.mas_left);
            make.right.equalTo(self.pictureIV.mas_right);
            make.height.equalTo(@20);
        }];
    }
    return _titleLB;
}

- (UIButton *)lookMoreButton {
    if (_lookMoreButton == nil) {
        _lookMoreButton = [[UIButton alloc] init];
        _lookMoreButton.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
        [_lookMoreButton setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
        [_lookMoreButton setTitleColor:MWHexColor(@"#333333") forState:UIControlStateHighlighted];
        [_lookMoreButton setTitle:[[MWConfigManager manager] objectForKey:@"view_more" defaultValue:@"查看更多"] forState:UIControlStateNormal];
        [_lookMoreButton setTitle:[[MWConfigManager manager] objectForKey:@"view_more" defaultValue:@"查看更多"] forState:UIControlStateHighlighted];
        _lookMoreButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _lookMoreButton.layer.cornerRadius = 5;
        _lookMoreButton.clipsToBounds = YES;
        [_lookMoreButton addTarget:self action:@selector(lookMoreButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_lookMoreButton];
        
        [_lookMoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLB.mas_bottom).offset(5);
            make.left.equalTo(self.titleLB.mas_left);
            make.height.equalTo(@(30));
            NSDictionary *btAtt = @{NSFontAttributeName : MWFont(13)};
            CGSize btSize = [[[MWConfigManager manager] objectForKey:@"view_more" defaultValue:@"查看更多"] sizeWithAttributes:btAtt];
            CGFloat with = btSize.width + 30;
            make.width.equalTo(@(with));
        }];
    }
    return _lookMoreButton;
}

#pragma mark - 查看更多的监听事件
- (void)lookMoreButtonClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(ShowRoomListCollectionViewCellLookMoreButtonClickWith:)]) {
        [self.delegate ShowRoomListCollectionViewCellLookMoreButtonClickWith:self.showroomModel];
    }
}

- (void)setShowroomModel:(MWShowroomModel *)showroomModel {
    _showroomModel = showroomModel;
    NSString *urlStr = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,h_240", showroomModel.image];
    [self.pictureIV sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"lunbo"]];
    self.titleLB.text = showroomModel.name;

}

@end
